//把rust结构体转为多种格式
use bincode::serialize as to_bincode;
use serde_cbor::to_vec as to_cbor;
use serde_derive::Serialize;
use serde_json::to_string as to_json;

#[derive(Serialize)] //让serde_derive软件报自动编写必要的代码，执行在内存中city和磁盘city的转换。
struct City {
    name: String,
    population: usize,
    latitude: f64,
    longitude: f64,
}
fn main() {
    let calabar = City {
        name: String::from("calabar"),
        population: 470_000,
        latitude: 4.95,
        longitude: 8.33,
    };
    let as_json = to_json(&calabar).unwrap();
    let as_cbor = to_cbor(&calabar).unwrap();
    let as_bincode = to_bincode(&calabar).unwrap();

    println!("json:\n{}\n", &as_json);
    println!("cbor:\n{:?}\n", &as_cbor);
    println!("bincode:\n{:?}\n", &as_bincode);

    println!(
        "json (as UTF-8):\n{}\n",
        String::from_utf8_lossy(as_json.as_bytes())
    );
    println!(
        "cbor (as UTF-8):\n{:?}\n",
        String::from_utf8_lossy(&as_cbor)
    );
    println!(
        "bincode (as UTF-8):\n{:?}\n",
        String::from_utf8_lossy(&as_bincode)
    );
}

//[dependencies]
// bincode ="1"
// serde="1"
// serde_cbor ="0.8"
// serde_derive="1"
// serde_json="1"
